using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 碰撞层预设所挂的脚本
/// 用于计算相应信息 以及自我旋转逻辑
/// </summary>
public class CollisionPrefab : MonoBehaviour
{
    public float pos
    {
        get
        {
            return transform.position.y;
        }
    }
    /// <summary>
    /// 当前的旋转角度
    /// </summary>
    public float currentAngle
    {
        get 
        {
            return transform.rotation.eulerAngles.z;
        }
    }

    /// <summary>
    /// 速度，度每秒
    /// </summary>
    public float speed = 0;

    private Image image = null;
    /// <summary>
    /// 得到夹角的大小
    /// </summary>
    public float angle
    {
        get
        {
            return image.fillAmount * 360;
        }
    }

    private float GetHeight()
    {
        if(height == 0)
        {
            Vector3[] v = new Vector3[4];
            transform.GetComponent<RectTransform>().GetWorldCorners(v);
            //print("999999999");
            //print(v[0]);
            //print(transform.position);
            //print(Vector2.Distance(v[0], transform.position));
            height = Vector2.Distance(v[0], transform.position) / Mathf.Sqrt(2);
            //print(height);
            //print("999999999");
        }

        return height;
    }

    /// <summary>
    /// 图片的高度，用于算出物理层级
    /// </summary>
    [HideInInspector]public float height = 0;

    /// <summary>
    /// 总开关控制是否旋转
    /// </summary>
    [HideInInspector] public bool canRotation = true;
    // private void Awake()
    // {
    //     
    //     image = transform.GetComponent<Image>();
    //     image.type = Image.Type.Filled;
    //     image.fillMethod = Image.FillMethod.Radial360;
    //     image.fillOrigin = 2;
    //     GetHeight();
    // }

    void Update()
    {
        if(canRotation)
        {
            transform.Rotate(Vector3.forward, -speed * Time.deltaTime);
            //print(transform.name +">>>>>>     "+ currentAngle);
        }
    }
    
    public void SetData(CollisonData data)
    {
        image = transform.GetComponent<Image>();
        image.type = Image.Type.Filled;
        image.fillMethod = Image.FillMethod.Radial360;
        image.fillOrigin = 2;
        
        // 设置配置层的信息
        image.fillAmount = data.fillAmount;
        speed = data.speed;
        transform.rotation = Quaternion.Euler(0, 0, data.eulerAngles);
        transform.GetComponent<RectTransform>().sizeDelta = new Vector2(data.size, data.size);
        
        GetHeight();
    } 
}